package servlet.admin;

import net.sf.json.JSONObject;
import service.admin.RulesService;
import service.admin.impl.RulesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * RulesDelServlet
 *
 * @author 谭文斌
 * @since 2023/9/5
 */
@WebServlet("/admin/rulesDelServlet")
public class RulesDelServlet extends HttpServlet {
    RulesService service = new RulesServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        PrintWriter out = resp.getWriter();
        String id = req.getParameter("id");

        int code = 1;
        String msg = "error";

        int del = service.del(Integer.valueOf(id));
        if (del == 1) {
            code=0;
            msg = "删除成功";
        }else {
            msg = "删除失败";
        }
        JSONObject json = new JSONObject();
        json.put("code",code);
        json.put("msg",msg);
        json.put("data",null);

        out.print(json.toString());
    }
}
